package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemNotice;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.SystemNoticeMapper;
import cn.wolfcode.trip.base.service.ISystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SystemNoticeServiceImpl implements ISystemNoticeService{
    @Autowired
    private SystemNoticeMapper noticeMapper;
    @Override
    public void addMessage(Travel travel) {
        SystemNotice systemNotice=new SystemNotice();
        if(travel.getState()==2){
            systemNotice.setMessage("已发布");
        }else if(travel.getState()==-1){
            systemNotice.setMessage("被拒绝");
        }else if (travel.getState()==1){
            systemNotice.setMessage("被取消发布");
        }
        systemNotice.setUser(travel.getAuthor());
        systemNotice.setTravel(travel);
        systemNotice.setCreateTime(new Date());
        systemNotice.setReadstate(false);
        noticeMapper.insert(systemNotice);
    }

    @Override
    public void addCommendMessage(TravelCommend travelCommend) {
            SystemNotice systemNotice=new SystemNotice();
            systemNotice.setTravel(travelCommend.getTravel());
            //systemNotice.getUser().setId(travel.getAuthor().getId());
            systemNotice.setUser(travelCommend.getTravel().getAuthor());
            systemNotice.setMessage("被推荐");
            systemNotice.setCreateTime(new Date());
            systemNotice.setReadstate(false);
            noticeMapper.insert(systemNotice);
    }

    @Override
    public Integer getNoticeCount(Long userId) {
        return noticeMapper.selectNoticeCount(userId);
    }

    @Override
    public List<SystemNotice> getNotices(Long userId) {
        return noticeMapper.selectNoticeInfo(userId);
    }

    @Override
    public void changeReadState(Long id) {
        noticeMapper.updateReadState(id);
    }
}
